package net.yxsoft.websocket.command;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;

import net.yxsoft.model.infrastructure.TsUser;
import net.yxsoft.service.infrastructure.LogService;
import net.yxsoft.service.infrastructure.LoginService;
import net.yxsoft.service.infrastructure.UserService;
import net.yxsoft.websocket.MessageServerIMP;
import rapid.core.Authuser;
import rapid.netty.command.JsonCommand;
import rapid.netty.command.RespCommand;
import rapid.util.StrKit;

/**
 * @author wj
 */
public class ImpCmd extends JsonCommand {
  @Override
  public void execute(ChannelGroup channelGroup, ChannelHandlerContext channelHandlerContext, JSONObject jsonObject) {

    String userId = jsonObject.getString("userId");
    // 检查参数是否完整
    if(StrKit.isEmpty(userId)) {
      response(channelHandlerContext, new RespCommand("ERRORIMP", "参数丢失！。").toJsonString());
      return;
    }

    UserService userService = new UserService();
    LoginService logService = new LoginService();
    TsUser user = userService.findById(userId);

    if(user != null) {
      MessageServerIMP.addUserCtx(channelHandlerContext,userId);
      MessageServerIMP.addUserId(userId,channelHandlerContext);


      MessageServerIMP.saveUser(logService.initAuthuser(user));

      response(channelHandlerContext, new RespCommand("MSGINFOIMP", "1").toJsonString());
    } else {
      response(channelHandlerContext, new RespCommand("ERROR", "用户不存在！。").toJsonString());
    }


  }
}
